package common

import (
	"time"
	"trader/models"

	"github.com/golang-jwt/jwt/v5"
)

// JWT配置
var (
	JwtSignKey string
	JwtExpire  time.Duration
)

// CustomClaims 自定义JWT Claims
type CustomClaims struct {
	UserId   int64  `json:"userId"`
	Username string `json:"username"`
	jwt.RegisteredClaims
}

// GenerateToken 生成JWT Token
func GenerateToken(user *models.User) (string, error) {
	claims := CustomClaims{
		UserId:   user.Id,
		Username: user.Username,
		RegisteredClaims: jwt.RegisteredClaims{
			ExpiresAt: jwt.NewNumericDate(time.Now().Add(JwtExpire)),
			Issuer:    "Trader",
		},
	}
	token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
	return token.SignedString([]byte(JwtSignKey))
}
